package com.company.corelib.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.company.corelib.model.Role;
import com.company.corelib.model.User;
import com.company.corelib.util.Util;

public class UserDAO {
	private Connection connect = null;
	private Statement statement = null;
	private PreparedStatement preparedStatement = null;
	private ResultSet rs = null;
	public List<User> getAllUser() {
		List<User> users = new ArrayList<User>();
		try {
			connect = Util.getConnection();
			statement = connect.createStatement();
			rs = statement
			          .executeQuery("SELECT userid, email, username, password, fullname, gender, likecount, lastlogin, status from tips.user");
			 while (rs.next()) {
				 User user = new User();
				 user.setId(Integer.parseInt(rs.getString("userid")));
				 user.setEmail(rs.getString("email"));
				 user.setUsername(rs.getString("username"));
				 user.setPassword(rs.getString("password"));
				 user.setFullname(rs.getString("fullname"));
				 user.setGender(rs.getString("gender"));
				 user.setLikecount(rs.getInt("likecount"));
				 user.setLikecount(rs.getInt("lastlogin"));
				 user.setStatus(rs.getInt("status"));
				 users.add(user);
			 }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			Util.close(connect,preparedStatement,rs);
		}
		return users;
	}
	public String create(User user) {
		String check = "true";
		System.err.println("user fullname = " + user.getFullname());
		try {
			connect = Util.getConnection();
			preparedStatement = connect
					.prepareStatement("insert into tips.user(email, username, password, fullname, gender, likecount, lastlogin, status,role) values(?,?,?,?,?,?,?,?,?)");
			preparedStatement.setString(1, user.getEmail());
			preparedStatement.setString(2, user.getUsername());
			preparedStatement.setString(3, user.getPassword());
			preparedStatement.setString(4, user.getFullname());
			preparedStatement.setString(5, user.getGender());
			preparedStatement.setInt(6, user.getLikecount());
			preparedStatement.setString(7, "");
			preparedStatement.setInt(8, user.getStatus());
			preparedStatement.setInt(9, user.getRole());
			preparedStatement.executeUpdate();
			check = "true";
		} catch (Exception e) {
			check = "false";
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			Util.close(connect,preparedStatement,rs);
		}
		return check;
	}
	public String update(User user) {
		String check = "true";
		try {
			connect = Util.getConnection();
			preparedStatement = connect
					.prepareStatement("update tips.user set email=?, username = ?, password = ?, fullname = ?, gender = ?, lastlogin = ?, status = ?, role= ? where userid=?");
			preparedStatement.setString(1, user.getEmail());
			preparedStatement.setString(2, user.getUsername());
			preparedStatement.setString(3, user.getPassword());
			preparedStatement.setString(4, user.getFullname());
			preparedStatement.setString(5, user.getGender());
			preparedStatement.setString(6, "");
			preparedStatement.setInt(7, user.getStatus());
			preparedStatement.setInt(8, user.getRole());
			preparedStatement.setInt(9, user.getId());
			System.err.println("preparedStatement = " + preparedStatement.toString());
			preparedStatement.executeUpdate();
			user.getId();
			check = "true";
		} catch (Exception e) {
			check = "false";
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			Util.close(connect,preparedStatement,rs);
		}
		System.err.println("Check = " + check);
		return check;
	}
	public User getUserById(String id) {
		User user = new User();
		try {
			connect = Util.getConnection();
			statement = connect.createStatement();
			rs = statement
			          .executeQuery("SELECT userid, email, username, password, fullname, gender, likecount, lastlogin, status, role from tips.user where userid="+id);
			 while (rs.next()) {
				 user.setId(Integer.parseInt(rs.getString("userid")));
				 user.setEmail(rs.getString("email"));
				 user.setUsername(rs.getString("username"));
				 user.setPassword(rs.getString("password"));
				 user.setFullname(rs.getString("fullname"));
				 user.setGender(rs.getString("gender"));
				 user.setLikecount(rs.getInt("likecount"));
				 user.setLikecount(rs.getInt("lastlogin"));
				 user.setStatus(rs.getInt("status"));
				 user.setRole(rs.getInt("role"));
			 }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			Util.close(connect,preparedStatement,rs);
		}
		System.err.println("user.getStatus() =" +user.getStatus());
		return user;
	}
	public String delete(String id) {
		String check = "true";
		try {
			connect = Util.getConnection();
			preparedStatement = connect
					.prepareStatement("delete from tips.user where userid=?");
			preparedStatement.setString(1, id);
			preparedStatement.executeUpdate();
			check = "true";
		} catch (Exception e) {
			check = "false";
			e.printStackTrace();
		} finally {
			Util.close(connect,preparedStatement,rs);
		}
		System.err.println("Check = " + check);
		return check;		
	}
	
	public List<Role> getAllRole() {
		List<Role> roles = new ArrayList<Role>();
		try {
			connect = Util.getConnection();
			statement = connect.createStatement();
			rs = statement
			          .executeQuery("SELECT roleid, code, name from tips.role");
			 while (rs.next()) {
				 Role role = new Role();
				 role.setRoleid(rs.getInt("roleid"));
				 role.setCode(rs.getString("code"));
				 role.setName(rs.getString("name"));
				 roles.add(role);
			 }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			Util.close(connect,preparedStatement,rs);
		}
		return roles;
	}
	public Role getRoleById(int roleId) {
		List<Role> roles = new ArrayList<Role>();
		Role role = new Role();
		try {
			connect = Util.getConnection();
			statement = connect.createStatement();
			rs = statement
			          .executeQuery("SELECT roleid, code, name from tips.role where roleid="+roleId);
			 while (rs.next()) {
				 role.setRoleid(rs.getInt("roleid"));
				 role.setCode(rs.getString("code"));
				 role.setName(rs.getString("name"));
				 roles.add(role);
			 }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			Util.close(connect,preparedStatement,rs);
		}
		return role;
	}
}
